# -*- coding: utf-8 -*-

# Created on 06/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class ControlManager:
    """ Manager for the game controls.
    
    * Methods:
    __init__(self)
    initialize(self)
    addControl(self, control)
    update(self)
    updateControls(self)
    readInterruptions(self)
    dispatchActions(self)
    
    * Member variables:
    controls -- Controls list.
    interruptions -- Interruptions list.
    
    """

    def __init__(self):
        """ Constructor.
        
        """
        self.controls = []
        #self.actions = []
        self.interruptions = []

    def initialize(self):
        """ Initializes the manager.
        
        """
        for c in self.controls:
            c.initialize()

    def addControl(self, control):
        """ Adds a control.
        
        * Arguments:
        control -- Control object.
        
        """
        self.controls.append(control)

    def update(self):
        """ Updates the manager: Updates the controls and interruptions, and \
        dispatches actions.
        
        """
        self.__updateControls()
        self.__readInterruptions()
        self.__dispatchActions()

    def __updateControls(self):
        """ Updates the controls.
        
        """
        for c in self.controls:
            c.update()

    def __readInterruptions(self):
        """ For each control, reads all its interruptions and appends them to
        self.interruptions list.
        
        """
        for c in self.controls:
            # Leer las interrupciones eliminandolas de la lista de interrupciones del control.
            # Despues, las leera la clase Game
            inter = c.readInterruption()
            while inter:
                self.interruptions.append(inter)
                inter = c.readInterruption()

    def __dispatchActions(self):
        """ For each control, dispatches its actions and makes its controllable
        actors to execute the actions.
        
        """
        for c in self.controls:
            action = c.readAction()
            while action:
                c.controllableActor.executeAction(action)
                action = c.readAction()

# eof
